<?php

require_once('interfaces/RequestMapping.interface.php');
require_once('classes/Mapping/AbstractMappingConfig.class.php');

class BasicProcessedMappingConfig extends AbstractMappingConfig implements RequestMapping{

    protected $from;
    protected $to;
    protected $parent;

    public function BasicProcessedMappingConfig($fromUri, $toFile){
        $to = $_SERVER['DOCUMENT_ROOT'].$toFile;
        if (file_exists($to)){
            $this->from = $fromUri;
            $this->to = $to;
            return true;
        }else{
            throw new Exception('Couldn\'t find the file');
        }
    }

    public function checkMapping($uri){
        if (!$this->configObj->conditionsMet()){
            return false;
        }
        if (!isset($extArray) || !is_array($extArray) || count($extArray) == 0){
            $extArray = array('');
        }
        foreach ($extArray as $ext){
            if ($this->from.$ext == $uri){
                return $this->abstractCheckMapping();
            }
        }
        return false;
    }

    public function run(){
        $this->configObj->run();
        $this->includeFile($this->to);
    }

    public static function interpretXmlConfig(/*XMLNodeList?*/ $item, /*RequestInterpreter*/ $interpreter){
        $tmp_doc = new DOMDocument();
        $tmp_doc->appendChild($tmp_doc->importNode($item,true));

        $from = $tmp_doc->getElementsByTagName('from')->item(0);
        $to = $tmp_doc->getElementsByTagName('to')->item(0);
        $name = $item->localName;
        $interpreter->addMapping(new $name($from->textContent,$to->textContent));
    }

}

?>